package com.technology.education.ui.student.popularize

import androidx.core.os.bundleOf
import com.angcyo.dsladapter.dpi
import com.angcyo.dsladapter.dslItem
import com.angcyo.dsladapter.toDslItemList
import com.angcyo.dsladapter.updateOrInsertHeaderItem
import com.google.android.material.imageview.ShapeableImageView
import com.technology.education.R
import com.technology.education.base.BaseRecyclerTopBarFragment
import com.technology.education.data.model.InviteList
import com.technology.education.dslitem.ItemSingleText
import com.technology.education.ext.*
import me.hgj.jetpackmvvm.ext.parseState

/**
 *@author:create by BZY
 *Date: 2022/3/2 07
 * 我的推广-我邀请的学生
 */
class PopularizeRecordFragment : BaseRecyclerTopBarFragment<PopularizeRecordViewModel>() {
    override fun initViews() {
        setTitle("我的推广")
        recycler.setBackgroundColor(getColor(R.color.bg_activity_gray))
    }

    override fun lazyLoadData() {
        loading()
        setUI()
    }

    private fun setUI() {
        renderAdapter {
            updateOrInsertHeaderItem<ItemSingleText>("title", 0) {
                it.apply {
                    style.topPadding = 24
                    style.isBold = true
                    style.itemBackColor = R.color.bg_activity_gray
                    title = "我邀请的学生"
                }
            }
        }
    }

    override fun createObserver() {
        mViewModel.result.observe(this) {
            parseState(it, onSuccess = { it ->
                hasMore(it.total)
                renderAdapter {
                    setPageItems(
                        it.list.toDslItemList(R.layout.item_popularize_record) {
                            itemBottomInsert = 12 * dpi
                            itemDecorationColor = R.color.bg_activity_gray
                            itemBindOverride = { itemHolder, _, _, _ ->
                                itemData?.let { it ->
                                    if (it is InviteList) {
                                        itemHolder.tv(R.id.tv_name)?.text = it.name
                                        itemHolder.tv(R.id.tv_info)?.text =
                                            "充值总金额: ${it.totalChargeAmount}"
                                        itemHolder.v<ShapeableImageView>(R.id.iv_header)?.apply {
                                            header(it.avatar, this)
                                        }
                                        itemHolder.clickItem { view ->
                                            navigation(
                                                view,
                                                R.id.action_popularizeRecordFragment_to_popularizeDetailFragment,
                                                bundleOf("info" to it)
                                            )
                                        }
                                    }
                                }
                            }
                        },
                        mViewModel.pageIndex
                    )
                }
            }, onError = { err -> toast(err.errorMsg) })
        }
    }
}